<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    {{pageTitle}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container" :style="{'min-height': containerHeight + 'px'}">
            <div slot="header" class="clearfix">
                <el-button type="primary" icon="el-icon-folder-add" @click="goAdd">新增</el-button>
                <!-- <el-button type="warning" icon="el-icon-edit" @click="goEdit">修改</el-button>
                <el-button type="danger" icon="el-icon-delete" @click="delAllSelection">删除</el-button> -->

                <!-- 搜索表单 --> 
                <!-- <el-form :model="query" ref="searchquery" label-width="100px">
                    <el-row style="margin-top:20px">
                        <el-col :sm="20" :md="5">
                            <el-form-item label="类别名称:" prop="giftName">
                                <el-input
                                    placeholder="请输入内容"
                                    v-model="query.giftName"
                                    size="small"
                                    class="handle-input mr10"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :sm="20" :md="5" style="margin-left:10px">
                            <el-button
                                type="primary"
                                icon="el-icon-search"
                                @click="submitForm('searchquery')"
                            >搜 索</el-button>
                            <el-button type="info" @click="resetForm('searchquery')">重 置</el-button>
                        </el-col>
                    </el-row>
                    
                </el-form> -->
            </div>
            <!-- 表格 -->
            <el-table
                :data="tableData"
                style="width: 100%;margin-bottom: 20px; margin-top: 20px"
                row-key="id"
                border
                default-expand-all
                :tree-props="{children: 'child', hasChildren: 'hasChildren'}">
                <el-table-column type="index" label="序号" align="center"></el-table-column>
                <el-table-column
                prop="name"
                label="姓名"
                sortable
                width="180">
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <a class="btn-a" @click="nodeEdit(scope.row.id)">修改</a>
                        <a class="btn-a" @click="nodeRemove(scope.row.id)">删除</a>
                    </template>
                </el-table-column>
            </el-table>
          
          
        </div>
    </div>
</template>


<script>
import appMain from '../../../utils/app_main';
export default {
    data() {
        return {
            pageTitle: '',
            containerHeight: '',
            query: {
                clientId: '', // 往来单位	
                clientName: '', // 往来单位名称	
                giftEndDate: '', // 发放查询结束日期	
                giftName: '', // 礼品名称	
                giftStartDate: '', // 发放查询开始日期	
                sortColumn: '', // 排序字段 倒序追加 columnName desc
            },
            filterText: '',
            onbordingTime:'',

            loading: false,
            tableData: [],
            multipleSelection: [],
        };
    },
    watch: {
      filterText(val) {
        this.$refs.tree.filter(val);
      }
    },
    mounted() {
        var div_height = window.screen.availHeight;
        var height = div_height - 100;
        this.containerHeight = height - 160;
        this.pageTitle = this.$router.currentRoute.meta.title;
    },
    methods: {
        // 组件选择完后把数据传过来
        selectFun(data) {
            this.multipleSelection = data.backData;
        },
        goEdit() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }
            console.log('goEdit', this.multipleSelection);
            this.$router.push({
                path: '/transitUnit',
                query: {
                    id: this.multipleSelection[0].id
                }
            });
        },
        goAdd() {
            this.$router.push({
                path: '/transitUnit'
            });
        },
        nodeEdit (id) {
            this.$router.push({
                path: '/transitUnit',
                query: {
                    id: id
                }
            });
        },
        nodeRemove (id, data) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    //请求接口删除数据
                    this.deleteById(id);
                })
                .catch((err) => {
                    debugger
                });
        },
        delAllSelection() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    //请求接口删除数据
                    this.deleteById(this.multipleSelection[0].id);
                })
                .catch((err) => {
                    debugger
                });
        },
        deleteById(id) {
            appMain
                .clientTypeDel({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.$message.success(res.data.message);
                        this.findAllPage();
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        //表格组件返回排序对象
        sortChange(data) {
            let column = data.backData;
            //排序
            if (column.order) {
                //倒序
                if (column.order === 'descending') {
                    this.query.sortColumn = column.prop + ' ' + 'desc';
                } else {
                    this.query.sortColumn = column.prop;
                }
            } else {
                //不排序
                this.query.sortColumn = '';
            }
            this.findAllPage();
        },
        //查询
        findAllPage() {
            this.loading = true;
            let params = { };
            appMain
                .clientTypeTypeTree(params)
                .then(res => {
                    if (res.data.code === 20000) {
                        //TODO 测试环境使用
                        this.timer = setTimeout(() => {
                            this.loading = false;
                        }, 1000);
                        this.tableData = res.data.data;
                        this.pagination.pageSize = res.data.data.pageSize;
                        this.pagination.pageIndex = res.data.data.pageNum;
                        this.pagination.pageTotal = res.data.data.total;
                    } else {
                        this.$message.error(res.data.message);
                        this.loading = false;
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        //分页导航
        pageChange(data) {
            this.pagination = data.backData;
            console.log('pageChange:', this.pagination);
            //分页变化--请求接口
            this.findAllPage();
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.findAllPage();
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        filterNode(value, data) {
            if (!value) return true;
            return data.name.indexOf(value) !== -1;
        }
    },
    created() {
        this.findAllPage();
    }
};
</script>

<style lang="css" scoped>
.el-card {
    height: 100%;
}
.custom-tree-node {
    /* min-width: 200px; */
    width: 100%;
    display: inline-block;
}
.custom-tree-node .btns {
    float: right;
}
.btn-a {
    color: #409eff;
    cursor: pointer;
    margin-left: 20px;
}
</style>